<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="编号名称" prop="numName">
        <el-input
          v-model="queryParams.numName"
          placeholder="请输入编号名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号编码" prop="numCode">
        <el-input
          v-model="queryParams.numCode"
          placeholder="请输入编号编码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前缀" prop="prefix">
        <el-input
          v-model="queryParams.prefix"
          placeholder="请输入前缀"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否需要日期" prop="needDate" >
        <el-select
          v-model="queryParams.needDate"
          placeholder="请选择是否需要日期"
          clearable filterable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期格式" prop="dateFormat">
        <el-select
          v-model="queryParams.dateFormat"
          placeholder="请选择日期格式"
          clearable filterable
          style="width: 240px"
        >
          <el-option
            v-for="dict in date_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="后缀" prop="suffix">
        <el-input
          v-model="queryParams.suffix"
          placeholder="请输入后缀"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目前编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入目前编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable filterable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:createNum:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
            type="success"
            plain
            icon="Edit"
            
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['transport:order:edit']"
            >编辑</el-button
          > -->
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:createNum:edit']"
          >编辑</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:createNum:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:createNum:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="createNumList"
      @selection-change="handleSelectionChange"
      border
      maxHeight="500"
      :header-cell-style="{ backgroundColor: '#F5F7FA' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="编号名称" align="center" prop="numName" />
      <el-table-column label="编号编码" align="center" prop="numCode" />
      <el-table-column label="前缀" align="center" prop="prefix" />
      <el-table-column label="日期格式" align="center" prop="dateFormat" />
      <el-table-column label="编号长度" align="center" prop="numLength" />
      <el-table-column label="目前编号" align="center" prop="num" />
      <!-- <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:createNum:edit']"
            >编辑</el-button
          >
          <el-button
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:createNum:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生成编号对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="编号名称" prop="numName" label-width="110px">
          <el-input v-model="form.numName" placeholder="请输入编号名称" />
        </el-form-item>
        <el-form-item label="编号编码" prop="numCode" label-width="110px">
          <el-input v-model="form.numCode" placeholder="请输入编号编码" />
        </el-form-item>
        <el-form-item label="前缀" prop="prefix" label-width="110px">
          <el-input v-model="form.prefix" placeholder="请输入前缀" />
        </el-form-item>
        <el-form-item label="是否需要日期" prop="needDate" label-width="110px">
          <el-select v-model="form.needDate" clearable filterable placeholder="请选择是否需要日期">
            <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期格式" label-width="110px">
          <el-select v-model="form.dateFormat" clearable filterable placeholder="请选择日期格式">
            <el-option
              v-for="dict in date_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="编号长度" prop="numLength" label-width="110px">
          <el-input v-model="form.numLength" placeholder="请输入编号长度" />
        </el-form-item>
        <el-form-item label="后缀" prop="suffix" label-width="110px">
          <el-input v-model="form.suffix" placeholder="请输入后缀" />
        </el-form-item>
        <el-form-item label="目前编号" prop="num" label-width="110px">
          <el-input v-model="form.num" placeholder="请输入目前编号" />
        </el-form-item>
        <el-form-item label="状态" label-width="110px">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :value="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCreateNum,
  getCreateNum,
  delCreateNum,
  addCreateNum,
  updateCreateNum,
  exportCreateNum,
} from "@/api/system/createNum";

export default {
  name: "CreateNum",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生成编号表格数据
      createNumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否需要日期字典
      sys_yes_no: [],
      // 状态字典
      sys_normal_disable: [],
      //日期格式
      date_type: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        numName: undefined,
        numCode: undefined,
        prefix: undefined,
        needDate: undefined,
        dateFormat: undefined,
        suffix: undefined,
        num: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        numName: [
          { required: true, message: "编号名称不能为空", trigger: "blur" },
        ],
        numCode: [
          { required: true, message: "编号编码不能为空", trigger: "blur" },
        ],
        needDate: [
          { required: true, message: "是否需要日期不能为空", trigger: "blur" },
        ],
        numLength: [
          { required: true, message: "编号长度不能为空", trigger: "blur" },
        ],
        num: [{ required: true, message: "目前编号不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    const { sys_yes_no, sys_normal_disable, date_type } = this.useDict(
      "sys_yes_no",
      "sys_normal_disable",
      "date_type"
    );
    this.sys_yes_no = sys_yes_no;
    this.sys_normal_disable = sys_normal_disable;
    this.date_type = date_type;
    this.getList();
  },
  methods: {
    /** 查询生成编号列表 */
    getList() {
      this.loading = true;
      listCreateNum(this.queryParams).then((response) => {
        this.createNumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否需要日期字典翻译
    needDateFormat(row, column) {
      return this.selectDictLabel(this.sys_yes_no, row.needDate);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.sys_normal_disable, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        numName: undefined,
        numCode: undefined,
        prefix: undefined,
        needDate: undefined,
        dateFormat: undefined,
        numLength: undefined,
        suffix: undefined,
        num: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生成编号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCreateNum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑生成编号";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCreateNum(this.form).then((response) => {
              if (response.code === 200) {
                this.$modal.msgSuccess("编辑成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCreateNum(this.form).then((response) => {
              if (response.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除生成编号编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delCreateNum(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生成编号数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportCreateNum(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>
